/* 
 * Copyright (C) 2008-2010 Josef Hardi <josef.hardi@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.bth.student.android.photohand.activity;

import edu.bth.student.android.photohand.provider.PhotoHandSchema.Albums;
import edu.bth.student.android.photohand.R;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class AlbumList extends ListActivity {

  private static final String TAG = "AlbumList";

  // Menu item ids
  private static final int REFRESH_ALBUM_ID = Menu.FIRST;
  private static final int DELETE_ALBUM_ID = Menu.FIRST + 1;
  private static final int EDIT_ALBUM_ID = Menu.FIRST + 2;
  private static final int ADD_ALBUM_ID = Menu.FIRST + 3;

  // Dialog ids
  private static final int ADD_ALBUM_DIALOG_ID = 1;

  /**
   * The columns we are interested in from the database
   */
  private static final String[] PROJECTION = new String[] { Albums._ID,
      Albums.TITLE, };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);

    // If no data was given in the intent (because we were started
    // as a MAIN activity), then use our default content provider.
    Intent intent = getIntent();
    if (intent.getData() == null) {
      intent.setData(Albums.CONTENT_URI);
    }

    // Inform the list we provide context menus for items
    getListView().setOnCreateContextMenuListener(this);

    // Perform a managed query. The Activity will handle closing and
    // re-querying the cursor when needed.
    Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null,
        Albums.DEFAULT_SORT_ORDER);

    if (cursor != null) {
      startManagingCursor(cursor);

      // Used to map notes entries from the database to views
      SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
          R.layout.album_list, cursor, new String[] { Albums.TITLE },
          new int[] { R.id.textview_album });
      setListAdapter(adapter);
    }
  }

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    // Remove the old menu
    menu.removeGroup(Menu.CATEGORY_SYSTEM);

    menu.add(Menu.CATEGORY_SYSTEM, REFRESH_ALBUM_ID, 0, "Refresh Album");
    menu.add(Menu.CATEGORY_SYSTEM, DELETE_ALBUM_ID, 1, "Delete Album");
    menu.add(Menu.CATEGORY_SYSTEM, EDIT_ALBUM_ID, 2, "Edit Album");
    menu.add(Menu.CATEGORY_SYSTEM, ADD_ALBUM_ID, 3, "Add Album");

    return true;
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Uri uri = Uri.withAppendedPath(ContentUris.withAppendedId(getIntent()
        .getData(), id), "photos");
    startActivity(new Intent(Intent.ACTION_VIEW, uri));
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case REFRESH_ALBUM_ID:
      /* TODO: Implement Refresh Album */
      return true;

    case DELETE_ALBUM_ID:
      /* TODO: Implement Delete Album */
      return true;

    case EDIT_ALBUM_ID:
      /* TODO: Implement Edit Album */
      return true;

    case ADD_ALBUM_ID:
      // Launch activity to insert a new album
      AlbumAdd.show(this);
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}
